/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Sindarin;

import Robot.Robot;
import Sindarin.CodeInterpreter.CodeInterpreter;
import Sindarin.Common.FileHeader;
import Sindarin.ScriptParser.ParseException;
import java.awt.AWTException;
import java.io.FileNotFoundException;
import Sindarin.Gui.Gui;
import Sindarin.ScriptParser.ScriptParser;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author user
 */
public class Main {

    public static final String OPTION_CHAR = "-";

    enum OPTION {

        NOGUI, H, FIN, FOUT, RUN, PASS, NATIVE
    };

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException, ClassNotFoundException, AWTException {
        String arg;
        ScriptParser scrParser = null;
        File fIn = null;
        File fOut = null;
        boolean gui = true;
        boolean run = false;
        boolean fin = false;
        boolean fout = false;
        boolean nat = false;
        FileHeader fHeader = new FileHeader();
        String pass = null;
        Robot robot = new Robot();

        fHeader.cypher = false;
        String searchtitle = null;
        for (int i = 0; i < args.length; i++) {
            arg = args[i];
            if (arg.startsWith(OPTION_CHAR)) {
                arg = arg.substring(1).toUpperCase();
                OPTION opt = null;
                try {
                    opt = OPTION.valueOf(arg.toUpperCase());
                } catch (IllegalArgumentException e) {
                    System.err.println("Error: ");
                    syntaxTxtMsg();
                    System.exit(-1);
                }

                switch (opt) {
                    case NOGUI:
                        gui = false;
                        break;
                    case RUN:
                        run = true;
                        break;
                    case H:
                        syntaxTxtMsg();
                        break;
                    case FIN:
                        String sIn = args[++i];
                        fIn = new File(sIn);
                        if (!(fIn.exists())) {
                            System.err.println(java.util.ResourceBundle.getBundle("Sindarin/Gui/config").getString("ERROR_FILE_NOT_FOUND") + " " + sIn);
                            syntaxTxtMsg();
                            System.exit(-1);
                        }
                        scrParser = new ScriptParser(new FileInputStream(fIn));
                        fin = true;
                        break;
                    case FOUT:
                        String sOut = args[++i];
                        fOut = new File(sOut);
                        fout = true;
                        break;
                    case PASS:
                        pass = args[++i];
                        fHeader.cypher = true;
                    case NATIVE:
                        if (robot.isNativeSystem()) {
                            searchtitle = args[++i];
                            nat = true;
                        } else {
                            System.err.println(new Exception("No native system"));
                            return;
                        }
                        break;
                }
            }
        }

        if (fin) {
            scrParser.inic();
            try {
                scrParser.begin();
            } catch (ParseException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (fout) {
                try {
                    scrParser.saveStateFile(fOut, fHeader, pass);
                } catch (Exception ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else {
                System.out.println(scrParser);
            }
        }
        if (nat) {
            HashMap natWin = robot.getAllVisibleWindow();
            String searchtitlelower = searchtitle.toLowerCase();
            for (Object handleW : natWin.keySet().toArray()) {
                String nameW = (String) natWin.get(handleW);
                if (nameW.toLowerCase().contains(searchtitlelower)) {
                    System.out.println("Match \"" + searchtitle + "\" in \"" + nameW + "\"");
                    robot.setActNativeHandle(handleW);
                }
            }
        }
        if (run) {
            CodeInterpreter vm = new CodeInterpreter(scrParser.getInstVector(), scrParser.getSymbolTable(), robot);
            vm.start();
        }
        if (gui) {
            Gui g = new Gui();
            g.setVisible(true);
        }

        return;
    }

    private static void syntaxTxtMsg() {
        System.out.println(java.util.ResourceBundle.getBundle("Sindarin/Gui/config").getString("TEXT_SYNTAX_MSG"));
    }
}
